import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app/openeye/widget/app_bar.dart';
import 'package:flutter_app/sqflite/user.dart';
import 'package:flutter_app/stateLearn/click_txt.dart';
import 'package:get/get.dart';
import 'package:get/get_rx/src/rx_types/rx_types.dart';
import 'package:sqflite/sqflite.dart';

import 'db_provider.dart';
import 'hand_sql_widget.dart';

class SqfLite extends StatefulWidget {
  const SqfLite({Key? key}) : super(key: key);

  @override
  _SqfLiteState createState() => _SqfLiteState();
}

class _SqfLiteState extends State<SqfLite> {
  var allUserList = <User>[].obs;
  var wereUserList = <User>[].obs;
  Database? db;
  String? versionStr;
  @override
  void initState() {
    super.initState();
    DBProvider().db.then((value) => db = value);
    rootBundle.loadString('assets/Version.json').then((value){
     print("获取到的版本信息$value");
      versionStr = value;
    });
  }
  @override
  Widget build(BuildContext context) {
    return materialPage(
        appBar: appBar("sqflite框架试用"), page: createSqfliteTestPage(context));
  }

  createSqfliteTestPage(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      child: Padding(
        padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
        child: ListView(
          children: [
            ClickTxt(info: "findAll", onPressed: () async {allUserList.value = await DBProvider().findAll();}),
            Obx(() {return allUserList.value.isEmpty ? const Text("未查询到数据") : createUserItem(allUserList.value);}),
            HandSql<String>(handInfo: "findOfWere(暂时只支持用年龄查询)", hand: (userName, age, sex)async {
              wereUserList.value = await DBProvider().find(age);
              print("查询年龄:${age}");
            }),
            Obx(() {
              print("重新建界面");
              return wereUserList.value.isEmpty ? const Text("未查询到数据") : createUserItem(wereUserList.value);
            }),
            HandSql<String>(handInfo: "add", hand: (userName, age, sex) {DBProvider().saveData(User(name: userName,age: age,sex: sex));}),
            HandSql<String>(
                handInfo: "delete",
                hand: (userName, age, sex) {
                  return Future.delayed(const Duration(milliseconds: 2), () {
                    return "这是删除数据的结果";
                  });
                }),
            HandSql<String>(
                handInfo: "change",
                hand: (userName, age, sex) {
                  return Future.delayed(const Duration(milliseconds: 2), () {
                    return "这是修改数据的结果";
                  });
                }),
            Column(
              children: [
                ClickTxt(info: "获取数据库版本号",onPressed: () async{
                  var version =await db!.getVersion();
                  Get.snackbar("弹窗", "数据库版本:$version",snackPosition:SnackPosition.BOTTOM,);
                },),
                ClickTxt(info: "重新设置数据库版本号",onPressed: () async{
                var dbVersion = await json.decode(versionStr!)['dbVersion'] as int;
                db!.setVersion(dbVersion);
                Get.snackbar("弹窗", "获取到的版本号:$dbVersion",snackPosition:SnackPosition.BOTTOM,);
                },),
              ],
            ),
          ],
        ),
      ),
    );
  }

  createUserItem(List<User> value) {
      return ListView.separated(
        itemBuilder: (context,index){
        var e = value[index];
        return Text(
                  "id:${e.id??'0'}\t\t"
                      "姓名:${e.name??'未知'}\t\t"
                      "年龄:${e.age??'0'}\t\t"
                      "性别:${e.sex == null?'':e.sex==1?"男":"女"}"
              );
      },
        separatorBuilder: (context,index)=> const Divider(),
        itemCount: value.length,);
      }

}
